﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.Web;
using HZ.Common;

namespace HZ.DrugTrade.Web.Enterprise.InvoiceManage
{
    public partial class UnInvoiceList : BasePage
    {
        public UnInvoiceList()
        {
            this.ModuleNo = Module.发票管理.ToString();
        }
        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetDefaultAreaString();
            if (!IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 绑定待开发票的医疗机构
        /// </summary>
        private void BindData()
        {
            IList<Model.Hospital> LisHospital = new List<Model.Hospital>();
            User user = CurrentAccount.GetUser();
            string where = "";
            if (!string.IsNullOrEmpty(GetAreaID()))
            {
                if (GetAreaID().Substring(2, 4).Equals("0000")) //代表省级别
                {
                    where = " and a.AreaID LIKE '" + GetAreaID().Substring(0, 2) + "%'";
                }
                else if (GetAreaID().Substring(4, 2).Equals("00")) //末尾是“00”代表市级
                {
                    where = "  and a.AreaID LIKE '" + GetAreaID().Substring(0, 4) + "%'";
                }
                else
                {
                    where = "  and a.AreaID = '" + GetAreaID() + "'";
                }
            }
            LisHospital = iPurchase.GetUnInvoiceHospitalList(user.Org.OrgID,this.txtHospitalName.Text.Trim(),where);
            gvwHospitalList.DataSource = LisHospital;
            gvwHospitalList.DataBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
        #region 获取地区信息
        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
        #endregion
    }
}
